package cn.melon.helper;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.UnknownHostException;

import cn.melon.bz.ClientThread;
import cn.melon.bz.ClientThreadMgr;
import cn.melon.common.Message;
import cn.melon.common.ModelType;

/**
 * 该类负责向服务器端发送请求
 * 
 * @author corvo
 */
public final class RequestHelper extends Thread{
    
    private ModelType modelType;
    private String request;
    private String UUID;
    private String recvID;
    private Message msg;
    
    /**
     * @param model 传入请求类型
     */
    public RequestHelper(ModelType model) {
        this.modelType = model;
        msg = new Message();
    }
    
    /**
     * 加入请求json字符串请求, 请求为json字符串
     * @param request       字符串请求
     * @return RequestHelper 返回本对象, 级联使用
     */
    public RequestHelper addRequest(String request) {
        this.request = request;
        return this;
    }
    
    /**
     * 构建msg中的内容
     * @return RequestHelper  返回本对象, 级联使用
     */
    public RequestHelper build() {
        msg.setData(this.request);
        msg.setModelType(this.modelType);
        msg.setUUID(this.UUID);
        msg.setReceicerID(this.recvID);
        return this;
    }
    
    /**
     * 添加回调函数, 回调函数需要确保recvID不同
     * 
     * @param recvID  用于确定回调函数, 请认真选择名字
     * @param listener      回调函数
     * @return RequestHelper 返回本对象, 级联调用
     */
    public RequestHelper addReceiver(String recvID, IOnRequestListener listener) {
        RequestMgr.setReceiver(recvID, listener);
        this.recvID = recvID;
        return this;
    }
    
    /**
     * 添加uuid, 暂时不使用
     * @return RequestHelper 级联调用
     */
    public RequestHelper addUUID() {
        ICacheHelper cache = new MemCacheHelper();
        this.UUID = cache.getCache("uuid");
        return this;
    }
    
    @Override
    public void run() {
        ClientThread thread = null;
        if (ModelType.LOGIN == modelType) {     // 登录请求单独进行
            try {
                ClientThreadMgr.remove("tmpThread");
                thread = ClientThreadMgr.get("tmpThread");      
                ObjectOutputStream oos = new ObjectOutputStream(thread.getOutputStream());
                if (!thread.isAlive()) {
                    thread.start();
                }

                oos.writeObject(msg);
                oos.flush();
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {                                // 其余请求
            ICacheHelper helper = new MemCacheHelper();
            //System.out.println(helper.getCache("uuid"));
            try {
                thread = ClientThreadMgr.get(helper.getCache("uuid"));
                if (thread == null) {
                    System.out.println("尚未登录");
                } else {
                    ObjectOutputStream oos = new ObjectOutputStream(thread.getOutputStream());
                    oos.writeObject(msg);
                    oos.flush();
                }
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
